package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constans.RedisConstant;
import com.atguigu.lease.common.excetion.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtils;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseEnum;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.captcha.SpecCaptcha;
import io.jsonwebtoken.Jwts;
import net.bytebuddy.implementation.bytecode.Throw;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.rmi.dgc.Lease;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private SystemUserService systemUserService;
    @Autowired
    private SystemUserMapper systemUserMapper;

    @Override
    public CaptchaVo getCpatcha() {

        SpecCaptcha code = new SpecCaptcha(130, 48, 4);
        code.setCharType(SpecCaptcha.TYPE_DEFAULT);
        String value = code.text().toLowerCase();
        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID();
        redisTemplate.opsForValue().set(key, value, RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);
        String iamge = code.toBase64();
        CaptchaVo vo = new CaptchaVo(iamge, key);
        return vo;

    }

    @Override
    public String login(LoginVo vo) {
        //思路:  信息获取要不前端传来 要不实体信息获取  死记住!!!

        //判断_验证码是否为空
        if (ObjectUtils.isEmpty(vo.getCaptchaCode())) {
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);
        }
        //判断_验证码是否过期
        if (!redisTemplate.hasKey(vo.getCaptchaKey())) {
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        //判断_验证码和redis是否一样
        String captcha_redis = (String) redisTemplate.opsForValue().get(vo.getCaptchaKey());
        if (!captcha_redis.equalsIgnoreCase(vo.getCaptchaCode())) {
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_ERROR);
        }
        //验证码所有验证通过后 删除该验证码
        redisTemplate.delete(vo.getCaptchaKey());

        LambdaQueryWrapper<SystemUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemUser::getUsername,vo.getUsername());
        SystemUser user = systemUserService.getOne(wrapper);
        //用户名是否一样
        //注意一点: !user.getUsername().equals(vo.getUsername())不可以这样写,容易空指针导致程序运行报错
        if (user==null){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        //用户名是否被禁用
        if (user.getStatus().equals(BaseStatus.DISABLE)){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }
        //检查密码是否正确
        if (!user.getPassword().equals(vo.getPassword())){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        //以上全部正确登录操作
        String token = JwtUtils.createToken(user.getId(), user.getUsername());
        return token;
    }


}
